package com.donzell.web.controller.admin;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.swing.plaf.TextUI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.donzell.dao.AdminUserDao;
import com.donzell.model.EcsAdminUser;
import com.donzell.utils.MD5Tools;
import com.donzell.web.controller.comm.Result;
import com.google.code.kaptcha.Constants;

@Controller
@RequestMapping("/admin/user")
public class AdminUserController {
	@Autowired
	private AdminUserDao adminUserDao;
	
	@ResponseBody
	@RequestMapping("/dologin")
	public Result doLogin(HttpServletRequest request){
		String username = request.getParameter("username");
		String userpass = request.getParameter("userpass");
		String checkcode = request.getParameter("checkcode");
		String ss_checkcode = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
//		if(!checkcode.equals(ss_checkcode))return Result.getErrResult("验证码不能为空");
		EcsAdminUser adminUser = adminUserDao.findByUserName(username);
		System.out.println("adminUser=>"+adminUser);
		System.out.println("ss_checkcode=>"+ss_checkcode);
		System.out.println("countByUserName=>"+adminUserDao.countByUserName(username));
		System.out.println("findAdminUserNumberByUserName=>"+adminUserDao.findAdminUserNumberByUserName(username));
		
		return Result.getSuccResult();
	}
	
	@ResponseBody
	@RequestMapping("/addadminuser")
	public Result addAdminUser(HttpServletRequest request){
		String username = request.getParameter("username");
		String userpass = request.getParameter("userpass");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String roleId = request.getParameter("roleId");
		String lastIp = request.getRemoteAddr();
//		if(!checkcode.equals(ss_checkcode))return Result.getErrResult("验证码不能为空");
		EcsAdminUser adminUser = new EcsAdminUser();
		adminUser.setUserName(username);
		adminUser.setPassword(MD5Tools.MD5(userpass));
		adminUser.setPhone(phone);
		adminUser.setEmail(email);
		adminUser.setRoleId(Short.valueOf(roleId));
		
		adminUser.setIsUse(Short.valueOf("1"));
		adminUser.setAddTime(new Date());
		adminUser.setLastLogin(new Date());
		adminUser.setLastIp(lastIp);
		adminUser = adminUserDao.save(adminUser);
		return Result.getSuccResult(adminUser);
	}
	
	
	@ResponseBody
	@RequestMapping("/changeuserstate")
	public Result getRoleList(HttpServletRequest request){
		String userId  = request.getParameter("userId");
		String isUse  = request.getParameter("isUse");
		if (isUse ==null || isUse.equals("")) {
			return Result.getErrResult("新的状态isUse不能为空");
		}
		short n_isUse = Short.parseShort(isUse);
		short n_userId = Short.parseShort(userId);
		adminUserDao.updateIsUse(n_userId,n_isUse);
		return Result.getSuccResult();
	}
	
}
